﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.BillPlugIn.BPW
{
    [Description("BPW:采购情况检查，采购情况检查记录"), HotUpdate]
    public class DzPurCheckQtyDate : AbstractBillPlugIn
    {
        public override void BeforeSave(BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);
            var entity = Model.BillBusinessInfo.GetEntity("FEntity");
            var rowData = Model.GetEntityDataObject(entity);
            var newRows = rowData.Where(x => Convert.ToInt32(x["F_Select"]) == 1);
            if (!(newRows.Count() > 0))
            {
                e.Cancel = true;
                return;
            }
            DeleteNotSeleteRows(this.Model);
        }

        private void DeleteNotSeleteRows(IDynamicFormModel model)
        {
            var entity = model.BillBusinessInfo.GetEntity("FEntity");
            var rowData = model.GetEntityDataObject(entity);
            for (int i = rowData.Count - 1; i >= 0; i--)
            {
                var isSelete = Convert.ToBoolean(rowData[i]["F_Select"]);
                if (!isSelete)
                {
                    model.DeleteEntryRow("FEntity", i);
                }
            }
        }
    }
}
